package fpt.edu.vn.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.model.Districts;
import fpt.edu.vn.model.Provinces;
import fpt.edu.vn.util.SolrQuerying;

/* 
 * DetailAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class SolrSearchAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private List<Object> mostView;
	private String Username;
	// Các tin đã xem
	private List<Object> recentView;

	private int city;
	private int district;
	private int area;
	private int price;
	private int topictypeid;
	private int sortby;
	private List<Provinces> provinces;
	private List<Districts> districts;
	private SolrQuerying solQ = new SolrQuerying();
	private List<Map<String, Object>> lstTopic = new ArrayList<Map<String, Object>>();
	private TopicsDao dao = new TopicsDao();

	// For SessionAware
	Map<String, Object> session;

	public String execute() {
		this.provinces = dao.listProvinces();
		this.districts = dao.listDistricts();
		this.mostView = dao.listMostView();
		String displayNamess = "";
		session = ActionContext.getContext().getSession();
		String ipaddress = "";

		if (session.containsKey("ipaddress")) {
			ipaddress = (String) session.get("ipaddress");
		}

		this.recentView = dao.listRecentView(ipaddress);
		if (session.containsKey("displayNamess")) {
			displayNamess = (String) session.get("displayNamess");
		}
		if ("".equals(displayNamess) || displayNamess == null) {
			setUsername(null);
		} else {
			setUsername(displayNamess);
		}
		try {
			solQ.updateDataFromMysqlToSolr();
			lstTopic = solQ.TopicSearch(city, district, topictypeid, price,
					area,sortby);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public List<Map<String, Object>> getLstTopic() {
		return lstTopic;
	}

	public void setLstTopic(List<Map<String, Object>> lstTopic) {
		this.lstTopic = lstTopic;
	}

	public Date getSysdate() {
		Date date = new Date();
		return date;
	}

	public String getStrDate() {
		String curdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(Calendar.getInstance().getTime());
		return curdate;
	}

	public List<Object> getMostView() {
		return mostView;
	}

	public void setMostView(List<Object> mostView) {
		this.mostView = mostView;
	}
	
	public int getSortby() {
		return sortby;
	}

	public void setSortby(int sortby) {
		this.sortby = sortby;
	}

	public List<Provinces> getProvinces() {
		return provinces;
	}

	public void setProvinces(List<Provinces> provinces) {
		this.provinces = provinces;
	}

	public String getUsername() {
		return Username;
	}

	public void setUsername(String username) {
		Username = username;
	}

	public int getCity() {
		return city;
	}

	public void setCity(int city) {
		this.city = city;
	}

	public int getArea() {
		return area;
	}

	public void setArea(int area) {
		this.area = area;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public TopicsDao getDao() {
		return dao;
	}

	public void setDao(TopicsDao dao) {
		this.dao = dao;
	}

	public int getDistrict() {
		return district;
	}

	public void setDistrict(int district) {
		this.district = district;
	}

	public List<Object> getRecentView() {
		return recentView;
	}

	public void setRecentView(List<Object> recentView) {
		this.recentView = recentView;
	}

	public int getTopictypeid() {
		return topictypeid;
	}

	public void setTopictypeid(int topictypeid) {
		this.topictypeid = topictypeid;
	}

	public List<Districts> getDistricts() {
		return districts;
	}

	public void setDistricts(List<Districts> districts) {
		this.districts = districts;
	}

}
